package com.open.capacity.finance.service.impl;

import com.open.capacity.finance.constant.SourceImportResult;
import com.open.capacity.finance.entity.*;
import com.open.capacity.finance.service.*;
import com.open.capacity.user.model.SysFile;
import com.open.capacity.user.service.SysFileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;
import com.open.capacity.commons.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import org.apache.commons.collections4.MapUtils;

import com.open.capacity.finance.dao.SourceFileImportDao;

@Slf4j
@Service
public class SourceFileImportServiceImpl implements SourceFileImportService {

    @Autowired
    private SourceFileImportDao sourceFileImportDao;

    @Autowired
    private SourceBaseUsecarService usecarService;

    @Autowired
    private SourceBasePayService payService;

    @Autowired
    private SourceBaseBusinessPayService businessPayService;

    @Autowired
    private SourceBaseBudgetExcuteService budgetExcuteService;

    @Autowired
    private SourceBaseBudgetTotal3Service budgetTotal3Service;

    @Autowired
    private SourceBaseBudgetTotal12Service budgetTotal12Service;

    @Autowired
    private SourceBaseFinalaccountTotal1Service finalaccountTotal1Service;
    @Autowired
    private SourceBaseFinalaccountTotal4Service finalaccountTotal4Service;
    @Autowired
    private SourceBaseFinalaccountTotal8Service finalaccountTotal8Service;

    @Autowired
    private SourceBaseHotelService hotelService;

    @Autowired
    private SourceBaseAssetService assetService;

    @Autowired
    private SourceBaseAssetTotalService assetTotalService;

    @Autowired
    private SourcePzService sourcePzService;
    @Autowired
    private SourcePzUnionService sourcePzUnionService;
    @Autowired
    private SourcePzPartyService sourcePzPartyService;

    @Autowired
    private SysFileService fileService;

    @Autowired
    private TaskExecutor taskExecutor;

    /**
     * 添加
     *
     * @param data
     */
    public int save(SourceFileImport data) {
        data.setUpdateTime(new Date());
        data.setImportProcess(SourceImportResult.DOING.getCode());

        List<SysFile> glUrlList = fileService.findByFolderId(data.getGlXml());
        String glUrl = glUrlList.size() == 0 ? "" : glUrlList.get(0).getUrl();

        List<SysFile> shareUrlList = fileService.findByFolderId(data.getShareXml());
        String shareUrl = shareUrlList.size() == 0 ? "" : shareUrlList.get(0).getUrl();

        List<SysFile> unionxszUrlList = fileService.findByFolderId(data.getUnionxszXls());
        String unionxszUrl = unionxszUrlList.size() == 0 ? "" : unionxszUrlList.get(0).getUrl();

        List<SysFile> partyxszUrlList = fileService.findByFolderId(data.getPartyxszXls());
        String partyxszUrl = partyxszUrlList.size() == 0 ? "" : partyxszUrlList.get(0).getUrl();

        List<SysFile> usecarUrlList = fileService.findByFolderId(data.getUsecarXls());
        String usecarUrl = usecarUrlList.size() == 0 ? "" : usecarUrlList.get(0).getUrl();

        List<SysFile> paydataUrlList = fileService.findByFolderId(data.getPaydataXls());
        String paydataUrl = paydataUrlList.size() == 0 ? "" : paydataUrlList.get(0).getUrl();

        List<SysFile> budgetexcuteUrlList = fileService.findByFolderId(data.getBudgetexcuteXls());
        String budgetexcuteUrl = budgetexcuteUrlList.size() == 0 ? "" : budgetexcuteUrlList.get(0).getUrl();

        List<SysFile> budgettotalUrlList = fileService.findByFolderId(data.getBudgettotalXls());
        String budgettotalUrl = budgettotalUrlList.size() == 0 ? "" : budgettotalUrlList.get(0).getUrl();

        List<SysFile> hoteldataUrlList = fileService.findByFolderId(data.getHoteldataXls());
        String hoteldataUrl = hoteldataUrlList.size() == 0 ? "" : hoteldataUrlList.get(0).getUrl();

        List<SysFile> assetdataUrlList = fileService.findByFolderId(data.getAssetdataXls());
        String assetdataUrl = assetdataUrlList.size() == 0 ? "" : assetdataUrlList.get(0).getUrl();

        List<SysFile> assettotalUrlList = fileService.findByFolderId(data.getAssettotalXls());
        String assettotalUrl = assettotalUrlList.size() == 0 ? "" : assettotalUrlList.get(0).getUrl();

        List<SysFile> businesspaydataUrlList = fileService.findByFolderId(data.getBusinesspaydataXls());
        String businesspaydataUrl = businesspaydataUrlList.size() == 0 ? "" : businesspaydataUrlList.get(0).getUrl();

        List<SysFile> finalaccountUrlList = fileService.findByFolderId(data.getFinalaccountXls());
        String finalaccountUrl = finalaccountUrlList.size() == 0 ? "" : finalaccountUrlList.get(0).getUrl();

        data.setGlXml(glUrlList.size() == 0 ? "" : glUrlList.get(0).getFolderId());
        data.setShareXml(shareUrlList.size() == 0 ? "" : shareUrlList.get(0).getFolderId());
        data.setUnionxszXls(unionxszUrlList.size() == 0 ? "" : unionxszUrlList.get(0).getFolderId());
        data.setPartyxszXls(partyxszUrlList.size() == 0 ? "" : partyxszUrlList.get(0).getFolderId());
        data.setUsecarXls(usecarUrlList.size() == 0 ? "" : usecarUrlList.get(0).getFolderId());
        data.setPaydataXls(paydataUrlList.size() == 0 ? "" : paydataUrlList.get(0).getFolderId());
        data.setBudgetexcuteXls(budgetexcuteUrlList.size() == 0 ? "" : budgetexcuteUrlList.get(0).getFolderId());
        data.setBudgettotalXls(budgettotalUrlList.size() == 0 ? "" : budgettotalUrlList.get(0).getFolderId());
        data.setHoteldataXls(hoteldataUrlList.size() == 0 ? "" : hoteldataUrlList.get(0).getFolderId());
        data.setAssetdataXls(assetdataUrlList.size() == 0 ? "" : assetdataUrlList.get(0).getFolderId());
        data.setAssettotalXls(assettotalUrlList.size() == 0 ? "" : assettotalUrlList.get(0).getFolderId());
        data.setBusinesspaydataXls(businesspaydataUrlList.size() == 0 ? "" : businesspaydataUrlList.get(0).getFolderId());
        data.setFinalaccountXls(finalaccountUrlList.size() == 0 ? "" : finalaccountUrlList.get(0).getFolderId());

        int key = sourceFileImportDao.save(data);

        //异步将离线文件写入数据库
        CompletableFuture.runAsync(() -> {
            try {
                if (StringUtils.isNotEmpty(glUrl)) {
                    SourceGLXML glxml = sourcePzService.parseGL(data.getCompanyId(), data.getKjnd(), glUrl);
                    sourcePzService.saveGL2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), glxml);
                }

                if (StringUtils.isNotEmpty(shareUrl)) {
                    SourceSHAREXML sharexml = sourcePzService.parseSHARE(data.getCompanyId(), data.getKjnd(), shareUrl);
                    sourcePzService.saveSHARE2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), sharexml);
                }

                //工会序时账
                if (StringUtils.isNotEmpty(unionxszUrl)) {
                    sourcePzUnionService.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), unionxszUrl);
                }

                //党费序时账
                if (StringUtils.isNotEmpty(partyxszUrl)) {
                    sourcePzPartyService.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), partyxszUrl);
                }

                //公务用车
                if (StringUtils.isNotEmpty(usecarUrl)) {
                    usecarService.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), usecarUrl);
                }

                //支付数据
                if (CollectionUtils.isNotEmpty(paydataUrlList)) {
                    payService.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), paydataUrlList);
                }

                //预算执行情况
                if (StringUtils.isNotEmpty(budgetexcuteUrl)) {
                    budgetExcuteService.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), budgetexcuteUrl);
                }

                //预算收入支出总表
                if (StringUtils.isNotEmpty(budgettotalUrl)) {
                    budgetTotal3Service.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), budgettotalUrl);
                    budgetTotal12Service.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), budgettotalUrl);
                }

                //会议酒店数据
                if (StringUtils.isNotEmpty(hoteldataUrl)) {
                    hotelService.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), hoteldataUrl);
                }

                //资产数据
                if (StringUtils.isNotEmpty(assetdataUrl)) {
                    assetService.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), assetdataUrl);
                }

                //资产汇总数据
                if (StringUtils.isNotEmpty(assettotalUrl)) {
                    assetTotalService.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), assettotalUrl);
                }

                //业务支付单据数据
                if (CollectionUtils.isNotEmpty(businesspaydataUrlList)) {
                    businessPayService.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), businesspaydataUrlList);
                }

                //部门决算数据
                if (StringUtils.isNotEmpty(finalaccountUrl)) {
                    finalaccountTotal1Service.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), finalaccountUrl);
                    finalaccountTotal4Service.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), finalaccountUrl);
                    finalaccountTotal8Service.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), finalaccountUrl);
                }

                data.setImportProcess(SourceImportResult.SUCCESS.getCode());
                sourceFileImportDao.update(data);

            } catch (Exception e) {
                data.setImportProcess(SourceImportResult.FAIL.getCode());
                sourceFileImportDao.update(data);
                log.error(e.toString());
            }
        }, taskExecutor);

        return 1;
    }

    /**
     * 修改
     *
     * @param data
     */
    public int update(SourceFileImport data) {
        SourceFileImport orig = sourceFileImportDao.findById(data.getId().toString());
        if (orig != null && !orig.getImportProcess().equals(-1)) {
            return 0;
        }

        data.setUpdateTime(new Date());
        data.setImportProcess(SourceImportResult.DOING.getCode());

//        List<SysFile> glUrlList = fileService.findByFolderId(data.getGlXml());
//        String glUrl = glUrlList.size() == 0 ? "" : glUrlList.get(0).getUrl();
//
//        List<SysFile> shareUrlList = fileService.findByFolderId(data.getShareXml());
//        String shareUrl = shareUrlList.size() == 0 ? "" : shareUrlList.get(0).getUrl();

//        List<SysFile> unionxszUrlList = fileService.findByFolderId(data.getUnionxszXls());
//        String unionxszUrl = unionxszUrlList.size() == 0 ? "" : unionxszUrlList.get(0).getUrl();
//
//        List<SysFile> partyxszUrlList = fileService.findByFolderId(data.getPartyxszXls());
//        String partyxszUrl = partyxszUrlList.size() == 0 ? "" : partyxszUrlList.get(0).getUrl();

        List<SysFile> usecarUrlList = fileService.findByFolderId(data.getUsecarXls());
        String usecarUrl = usecarUrlList.size() == 0 ? "" : usecarUrlList.get(0).getUrl();

//        List<SysFile> paydataUrlList = fileService.findByFolderId(data.getPaydataXls());
//        String paydataUrl = paydataUrlList.size() == 0 ? "" : paydataUrlList.get(0).getUrl();

        List<SysFile> budgetexcuteUrlList = fileService.findByFolderId(data.getBudgetexcuteXls());
        String budgetexcuteUrl = budgetexcuteUrlList.size() == 0 ? "" : budgetexcuteUrlList.get(0).getUrl();

        List<SysFile> budgettotalUrlList = fileService.findByFolderId(data.getBudgettotalXls());
        String budgettotalUrl = budgettotalUrlList.size() == 0 ? "" : budgettotalUrlList.get(0).getUrl();

        List<SysFile> hoteldataUrlList = fileService.findByFolderId(data.getHoteldataXls());
        String hoteldataUrl = hoteldataUrlList.size() == 0 ? "" : hoteldataUrlList.get(0).getUrl();

        List<SysFile> assetdataUrlList = fileService.findByFolderId(data.getAssetdataXls());
        String assetdataUrl = assetdataUrlList.size() == 0 ? "" : assetdataUrlList.get(0).getUrl();

        List<SysFile> assettotalUrlList = fileService.findByFolderId(data.getAssettotalXls());
        String assettotalUrl = assettotalUrlList.size() == 0 ? "" : assettotalUrlList.get(0).getUrl();

//        List<SysFile> businesspaydataUrlList = fileService.findByFolderId(data.getBusinesspaydataXls());
//        String businesspaydataUrl = businesspaydataUrlList.size() == 0 ? "" : businesspaydataUrlList.get(0).getUrl();

        List<SysFile> finalaccountUrlList = fileService.findByFolderId(data.getFinalaccountXls());
        String finalaccountUrl = finalaccountUrlList.size() == 0 ? "" : finalaccountUrlList.get(0).getUrl();

//        data.setGlXml(glUrlList.size() == 0 ? "" : glUrlList.get(0).getFolderId());
//        data.setShareXml(shareUrlList.size() == 0 ? "" : shareUrlList.get(0).getFolderId());
//        data.setUnionxszXls(unionxszUrlList.size() == 0 ? "" : unionxszUrlList.get(0).getFolderId());
//        data.setPartyxszXls(partyxszUrlList.size() == 0 ? "" : partyxszUrlList.get(0).getFolderId());
        data.setUsecarXls(usecarUrlList.size() == 0 ? "" : usecarUrlList.get(0).getFolderId());
//        data.setPaydataXls(paydataUrlList.size() == 0 ? "" : paydataUrlList.get(0).getFolderId());
        data.setBudgetexcuteXls(budgetexcuteUrlList.size() == 0 ? "" : budgetexcuteUrlList.get(0).getFolderId());
        data.setBudgettotalXls(budgettotalUrlList.size() == 0 ? "" : budgettotalUrlList.get(0).getFolderId());
        data.setHoteldataXls(hoteldataUrlList.size() == 0 ? "" : hoteldataUrlList.get(0).getFolderId());
        data.setAssetdataXls(assetdataUrlList.size() == 0 ? "" : assetdataUrlList.get(0).getFolderId());
        data.setAssettotalXls(assettotalUrlList.size() == 0 ? "" : assettotalUrlList.get(0).getFolderId());
//        data.setBusinesspaydataXls(businesspaydataUrlList.size() == 0 ? "" : businesspaydataUrlList.get(0).getFolderId());
        data.setFinalaccountXls(finalaccountUrlList.size() == 0 ? "" : finalaccountUrlList.get(0).getFolderId());

        int key = sourceFileImportDao.update(data);

        //异步将离线文件写入数据库
        CompletableFuture.runAsync(() -> {
            try {
//                if (StringUtils.isNotEmpty(glUrl)) {
//                    SourceGLXML glxml = sourcePzService.parseGL(data.getCompanyId(), data.getKjnd(), glUrl);
//                    sourcePzService.saveGL2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), glxml);
//                }
//
//                if (StringUtils.isNotEmpty(shareUrl)) {
//                    SourceSHAREXML sharexml = sourcePzService.parseSHARE(data.getCompanyId(), data.getKjnd(), shareUrl);
//                    sourcePzService.saveSHARE2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), sharexml);
//                }
//
//                //工会序时账
//                if (StringUtils.isNotEmpty(unionxszUrl)) {
//                    sourcePzUnionService.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), unionxszUrl);
//                }
//
//                //党费序时账
//                if (StringUtils.isNotEmpty(partyxszUrl)) {
//                    sourcePzPartyService.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), partyxszUrl);
//                }

                //公务用车
                if (StringUtils.isNotEmpty(usecarUrl)) {
                    usecarService.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), usecarUrl);
                }

//                //支付数据
//                if (CollectionUtils.isNotEmpty(paydataUrlList)) {
//                    payService.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), paydataUrlList);
//                }

                //预算执行情况
                if (StringUtils.isNotEmpty(budgetexcuteUrl)) {
                    budgetExcuteService.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), budgetexcuteUrl);
                }

                //预算收入支出总表
                if (StringUtils.isNotEmpty(budgettotalUrl)) {
                    budgetTotal3Service.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), budgettotalUrl);
                    budgetTotal12Service.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), budgettotalUrl);
                }

                //会议酒店数据
                if (StringUtils.isNotEmpty(hoteldataUrl)) {
                    hotelService.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), hoteldataUrl);
                }

                //资产数据
                if (StringUtils.isNotEmpty(assetdataUrl)) {
                    assetService.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), assetdataUrl);
                }

                //资产汇总数据
                if (StringUtils.isNotEmpty(assettotalUrl)) {
                    assetTotalService.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), assettotalUrl);
                }

//                //业务支付单据数据
//                if (CollectionUtils.isNotEmpty(businesspaydataUrlList)) {
//                    businessPayService.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), businesspaydataUrlList);
//                }

                //部门决算数据
                if (StringUtils.isNotEmpty(finalaccountUrl)) {
                    finalaccountTotal1Service.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), finalaccountUrl);
                    finalaccountTotal4Service.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), finalaccountUrl);
                    finalaccountTotal8Service.save2db(data.getCompanyId(), data.getKjnd(), data.getImportWay(), finalaccountUrl);
                }

                data.setImportProcess(SourceImportResult.SUCCESS.getCode());
                sourceFileImportDao.update(data);

            } catch (Exception e) {
                data.setImportProcess(SourceImportResult.FAIL.getCode());
                sourceFileImportDao.update(data);
                log.error(e.toString());
            }
        }, taskExecutor);

        return 1;
    }


    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        return sourceFileImportDao.delete(id);
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public SourceFileImport findById(String id) {
        return sourceFileImportDao.findById(id);
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<SourceFileImport> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<SourceFileImport> list = sourceFileImportDao.findAll(params);
        PageInfo<SourceFileImport> pageInfo = new PageInfo(list);

        return PageResult.<SourceFileImport>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<SourceFileImport> findAll(Map<String, Object> params) {
        return sourceFileImportDao.findAll(params);
    }

}
